{% extends "_baseModel.tpl" %}
{% block body %}

{% if flags["add-includes"] %}

require_once "{{ route.getLib('ParserGenerator') }}";

{% endif %}
/**
 * Clase {{ Collection }} que representa una collección de objetos {{ Bean }}
 *
 * @category   lib
 * @package    lib_models
 * @subpackage lib_models_collections
 * @copyright  {{ copyright }}
{% if showBenderSignature %}
 * @copyright  {{ benderSignature }}
 * @author     zetta & chentepixtol
{% endif %}
 * @version    {{ version }} SVN: $Revision$
 */
class {{ Collection }} extends ArrayIterator
{

    /**
     * @var Parser
     */
    private $parser;
    
    /**
     * Constructor
     * @param array $array
     * @return void
     */
    public function __construct($array = array())
    {
        $this->parser = new Parser('{{ Bean }}');
        parent::__construct($array);
    }

    /**
     * Appends the value
     * @param {{ Bean }} ${{ bean }}
     */
    public function append({{ Bean }} ${{ bean }})
    {
{% if table.hasPrimaryField() %}
        parent::offsetSet(${{ bean }}->{{ table.getPrimaryField().getGetterName() }}(), ${{ bean }});
        $this->rewind();
{% else %}
        parent::append(${{ bean }});
        $this->rewind();
{% endif %}
    }

    /**
     * Return current array entry
     * @return {{ Bean }}
     */
    public function current()
    {
        return parent::current();
    }

    /**
     * Return current array entry and 
     * move to next entry
     * @return {{ Bean }} 
     */
    public function read()
    {
        ${{ bean }} = $this->current();
        $this->next();
        return ${{ bean }};
    }

    /**
     * Get the first array entry
     * if exists or null if not 
     * @return {{ Bean }}|null 
     */
    public function getOne()
    {
        if ($this->count() > 0)
        {
            $this->seek(0);
            return $this->current();
        } else
            return null;
    }
    
{% if table.hasPrimaryField() %}
    /**
     * Contains one object with ${{ table.getPrimaryField().getPhpName() }}
     * @param  {{ table.getPrimaryField().getDataType() }} ${{ table.getPrimaryField().getPhpName() }}
     * @return boolean
     */
    public function contains(${{ table.getPrimaryField().getPhpName() }})
    {
        return parent::offsetExists(${{ table.getPrimaryField().getPhpName() }});
    }
    
    /**
     * Merge two Collections
     * @param {{ Collection }} ${{ collection }}
     * @return void
     */
    public function merge({{ Collection }} ${{ collection }})
    {
        while(${{ collection }}->valid())
        {
            ${{ bean }} = ${{ collection }}->read();
            if( !$this->contains( ${{ bean }}->{{ table.getPrimaryField().getGetterName() }}() ) )
            {
                $this->append(${{ bean }});
            }             
        }
        ${{ collection }}->rewind();
    }
    
    /**
     * Retrieve the array with primary keys 
     * @return array
     */
    public function getPrimaryKeys()
    {
        return array_keys($this->getArrayCopy());
    }
    
    /**
     * Retrieve the {{ Bean }} with primary key  
     * @param  {{ table.getPrimaryField().getDataType() }} ${{ table.getPrimaryField().getPhpName() }}
     * @return {{ Bean }}
     */
    public function getByPK(${{ table.getPrimaryField().getPhpName() }})
    {
        return $this->contains(${{ table.getPrimaryField().getPhpName() }}) ? $this[${{ table.getPrimaryField().getPhpName() }}] : null;
    }
{% endif %}
  
    /**
     * Transforma una collection a un array
     * @return array
     */
    public function toArray()
    {
        $array = array();
        while ($this->valid())
        {
            ${{ bean }} = $this->read();
            $this->parser->changeBean(${{ bean }});
{% if table.hasPrimaryField() %}
            $array[${{ bean }}->{{ table.getPrimaryField().getGetterName() }}()] = $this->parser->toArray();
{% else %}
            $array[] = $this->parser->toArray();
{% endif %}
        }
        $this->rewind();
        return $array;
    }
    
    /**
     * Crea un array asociativo de $key => $value a partir de las constantes de un bean
     * @param string $ckey
     * @param string $cvalue
     * @return array
     */
    public function toKeyValueArray($ckey, $cvalue)
    {
        $array = array();
        while ($this->valid())
        {
            ${{ bean }} = $this->read();
            $this->parser->changeBean(${{ bean }});
            $array += $this->parser->toKeyValueArray($ckey, $cvalue);
        }
        $this->rewind();
        return $array;
    }
    
    /**
     * Retrieve the parser object
     * @return Parser
     */
    public function getParser()
    {
        return $this->parser;
    }
  
  
}

{% endblock %}
